package com.example.GroovyScriptExample.controller;

import java.util.Date;

//import javax.script.Invocable;
//import javax.script.ScriptContext;
//import javax.script.ScriptEngine;
//import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

@RestController
@RequestMapping(value = "/groovy")
public class GroovyScriptController {
//	ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
//	Invocable invoke = null;
	Binding binding = new Binding();  
    GroovyShell shell = new GroovyShell(binding);  
	@RequestMapping(value = "/toParse", method = RequestMethod.POST)
	public String parseScript(String pamarstr, String dnumstr, HttpServletRequest request)
			throws ScriptException, NoSuchMethodException {
		String retstr = "";

		Double dnum = Double.valueOf(dnumstr);
		System.out.println("Params:--------Date:" + new Date().toString() + "------" + "pamarstr:" + pamarstr + "----" + "dnum:" + dnum);
		if (null == pamarstr || pamarstr.length() == 0) {
			pamarstr = "123456789";
			System.out.println("paramstr：数据传入为空，修改为默认值 123456789");
		}
		if (null == dnum || dnum == 0.0) {
			dnum = 123456789.0;
			System.out.println("dnum：数据传入为空或为0，修改为默认值 123456789.0");
		}
//		String script = sbr.toString().replaceAll("\\$\\{pamarstr\\}", pamarstr).replaceAll("\\$\\{dnum\\}",
//				String.valueOf(dnum));
//		engine.put("pamarstr", pamarstr);
//		engine.put("dnum", dnum);
//		ScriptContext context = engine.getContext();
//		ScriptContext.ENGINE_SCOPE;
//		ScriptContext.GLOBAL_SCOPE;
//		context.setAttribute("pamarstr", pamarstr, ScriptContext.ENGINE_SCOPE);
//		context.setAttribute("dnum", dnum, ScriptContext.ENGINE_SCOPE);
//		engine.setContext(context);
//		engine.eval(sbr);// resultstr
		binding.setVariable("pamarstr", pamarstr);
		binding.setVariable("dnum", dnum); 
		retstr = (String) shell.evaluate(sbr);
//		retstr = (String) engine.get("resultstr");
//		invoke = (Invocable) engine;
//		System.out.println(invoke.toString());

//		retstr = String.valueOf(invoke.invokeFunction("fun_groovy"));
		System.out.println("Return:--------" +retstr);
		return retstr;
	}

	/**
	 * 获取字符串参数 String 的前 3个字符以及后 3个字符 然后拼接在 ":字符串变量:" 该字符串左右， 对double 进行 乘方 操作
	 * 并把两个参数的结果 通过 ";" 拼接起来成为新的字符串并返回 这里为了凸显 groovy 的特性 特地使用了Java代码执行截取前三个字符
	 */
//	String sbr = new String("class rfeng{def fun_groovy(){ " + "String str=\":字符串变量:\"	; "
//			+ "String start3 = \"${pamarstr}\".substring(0,3); " + "String end3 = \"${pamarstr}\"[-3..-1]; "
//			+ "str = start3+str+end3; " + "double dnum = ${dnum}; " + "return str+\";\"+Math.pow(dnum,2);" + "}}");

//	String sbr = new String("" + "str=\":字符串变量:\"	; " + "start3 = \"${pamarstr}\".substring(0,3);\r\n "
//			+ "end3 = \"${pamarstr}\"[-3..-1]; \r\n" + "str = start3+str+end3; \r\n " + "dnum = ${dnum}; \r\n"
//			+ "println str+\";\"+Math.pow(dnum,2);" + "");

	String sbr = new String("def fun_groovy(){ " 
	+ "String str=\":字符串变量:\"	; "
	+ "String start3 = pamarstr.substring(0,3); " + "String end3 = pamarstr[-3..-1]; "
	+ "str = start3+str+end3; " + "" + "return str+\";\"+Math.pow(dnum,2);" 
						+ "};fun_groovy();");

//	String sbr = new String("" + "str=\":字符串变量:\"	; "
//			+ "start3 = \"${pamarstr}\".substring(0,3); " + "end3 = \"${pamarstr}\"[-3..-1]; "
//			+ "str = start3+str+end3; " + "dnum = ${dnum}; " + "def resultstr = str+\";\"+Math.pow(dnum,2);" + "");

//	public static void main(String[] args) {
//		ScriptEngineManager manager = new ScriptEngineManager();
//		ScriptEngine engine = manager.getEngineByName("groovy");
//		System.out.println("Calling script from Java");
//		try {
//			engine.eval("println 'Hello from Groovy'");
//		} catch (ScriptException ex) {
//			System.out.println(ex);
//		}
//	}
}
